/// this is the implementation of the stack using linked list, the 1st node is always top here


#include<iostream.h>
#include<conio.h>
#include"Node.h"
class Stack
{
      private:
               void push(Node **top, int value);
               void display(Node *top);
               int Top(Node *top);
               bool isEmptyStack(Node *top);
               void deleteStack(Node **top);
      public:
             struct Node *top;
             Stack()
             {
                    top = NULL;
              }
             void display();
             void push(int value);
             void pop();
             int Top();
             bool isEmptyStack();
             void deleteStack();
             
};
void Stack :: deleteStack(Node **top)
{
     Node *tempNode = *top;
     while(tempNode->next)
     {
            Node *delNode = tempNode->next;
           tempNode = *top;
           delete (delNode);
     }
     delete (*top);
}
void Stack :: deleteStack()
{
     deleteStack(&top);
}
bool Stack :: isEmptyStack(Node *top)
{
     return (top == NULL);
}
bool Stack :: isEmptyStack()
{
     return  isEmptyStack(top);
}
int Stack :: Top(Node *top)
{
    Node *tempNode = top;
    return tempNode->data;
}
int Stack :: Top()
{
    Top(top);
}

void Stack :: display(Node *top)
{
     Node *currNode = top;
     if(currNode == NULL)
          return ;
     else
     {
         cout<<" "<<currNode->data;
         display(currNode->next);
     }
}
void Stack :: display()
{
     display(top);
}
void Stack :: push(Node **top, int value)
{
     Node *newNode = new Node(value);
     if(*top == NULL)
     {
             *top = newNode;
     }
     else
     {
         Node *currNode = *top;
         newNode->next = currNode;
         *top = newNode;        
     }
}
void Stack :: push(int value)
{
     push(&top,value);
}
int main()
{
    Stack stak;
    stak.push(5);
    stak.push(15);
    stak.push(53);
    stak.push(511);
    stak.display(); 
    cout<<"\n top = "<<stak.Top();
    cout<<"\n isEmptyStack = "<<stak.isEmptyStack();
    stak.deleteStack();
    stak.display(); 
    getch();
    return 0;
}
